package top.lanscarlos.ashcraft.repository;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import top.lanscarlos.ashcraft.pojo.Commodity;
import top.lanscarlos.ashcraft.pojo.Seller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft.repository
 *
 * @author Lanscarlos
 * @since 2022-05-26 17:55
 */
@Repository
public class CommodityRepository {
    @Autowired
    private SessionFactory sessionFactory;

    public Commodity getCommodity(int commodityId) {
        Commodity commodity = null;
        Session session = sessionFactory.openSession();
        try {
            commodity = session.get(Commodity.class, commodityId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return commodity;
    }

    public List<Commodity> query() {
        return query(null, 0);
    }

    public List<Commodity> query(String name, int sellerId) {
        List<Commodity> commodities = new ArrayList<>();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        try {
            CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<Commodity> criteria = builder.createQuery(Commodity.class);
            Root<Commodity> root = criteria.from(Commodity.class);

            criteria.select(root);
            if (name != null) {
                criteria.where(builder.like(root.get("name"), name + "%"));
            }

            commodities = session.createQuery(criteria).getResultList();

            if (sellerId > 0) {
                for (int i=0; i<commodities.size(); i++) {
                    if (commodities.get(i).getSeller().getId() != sellerId) {
                        commodities.remove(i--);
                    }
                }
            }

            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return commodities;
    }

    public boolean insert(int sellerId, Commodity commodity) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        try {
            Seller seller = session.get(Seller.class, sellerId);
            commodity.setSeller(seller);
//            seller.getCommodities().add(commodity);
            session.save(commodity);
            transaction.commit();
            return true;
        } catch (Exception e) {
            if (transaction != null) transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return false;
    }
}
